package com.emellend.api.list;


import com.emellend.api.R;
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.view.View;
import android.widget.AdapterView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class List7 extends ListActivity implements OnItemSelectedListener {
	private TextView mPhone;
	private static final String[] PHONE_PROJECTION={
		Phone._ID,
		Phone.TYPE,
		Phone.LABEL,
		Phone.NUMBER,
		Phone.DISPLAY_NAME
	};
	
	private static final int COLUMN_PHONE_TYPE = 1;
    private static final int COLUMN_PHONE_LABEL = 2;
    private static final int COLUMN_PHONE_NUMBER = 3;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_7);
		mPhone=(TextView) this.findViewById(R.id.phone);
		getListView().setOnItemSelectedListener(this);
		Cursor cursor = getContentResolver().query(Phone.CONTENT_URI, PHONE_PROJECTION, Phone.NUMBER+" NOT NULL", null, null);
		startManagingCursor(cursor);
		setListAdapter(new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, 
				new String[]{Phone.DISPLAY_NAME}, new int[]{android.R.id.text1}));
	}

	public void onItemSelected(AdapterView<?> parent, View view, int position,
			long id) {
		if (position>=0) {
			Cursor cursor = (Cursor) parent.getItemAtPosition(position);
			int type=cursor.getInt(COLUMN_PHONE_TYPE);
			String phone = cursor.getString(COLUMN_PHONE_NUMBER);
			String label=null;
			if (type==Phone.TYPE_CUSTOM) {
				label=cursor.getString(COLUMN_PHONE_LABEL);
			}
			String numberType = Phone.getTypeLabel(getResources(), type, label).toString();
			String text=numberType+" :"+phone;
			mPhone.setText(text);
		}
		
	}

	public void onNothingSelected(AdapterView<?> parent) {}
}
